﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AmuTools;
using app.Api;

namespace app.Models
{
    [Model(TableName = "t_member", PrimaryKey = "id", IdentityInsert = false, BaseID = "200000")]
    public class Member : ModelBase
    {
        /*
         *企业名称
         * 企业简介
         * 注册资本
         * 
        */

        public int id { get; set; }

        public string username { get; set; }

        [Field(Webable = false, DataType = "varchar(50)")]
        public string password { get; set; }

        [Field(Webable = false, DataType = "char(11)")]
        public string h_mobile_phone { get; set; }

        [Field(Storageable = false)]
        public string mobile_phone {
            get
            {
                return h_mobile_phone;
            }
        }

        public string role_type { get; set; }

        [Field(DataType = "varchar(100)")]
        public string role_ids { get; set; }

        [Field(Webable = false, DataType = "text")]
        public string special_permission { get; set; }

        private string _permission = "";
        [Field(Storageable = false)]
        public string permission
        {
            get
            {
                if (_permission == null)
                {
                    Member member = G.DB.GetById<Member>(id);
                    SqlResult result = G.DB.Get(string.Format("select * from {0} where '{1}' like '%'+convert(varchar,id)+'%'", G.DB.GetTableName<AdminRole>(), member.role_ids));
                    List<AdminRole> roles = result.GetFirstTableList<AdminRole>();
                    string permission = "";
                    foreach (AdminRole item in roles)
                    {
                        permission += item.permission;
                    }
                    _permission = permission + this.special_permission;
                }
                return _permission;
            }
        }

        public int status { get; set; }

        [Field(Webable = false)]
        public int is_delete { get; set; }

        [Field(Storageable = false)]
        public int landing_state { get; set; }

        public static Member[] GetInitData()
        {
            return new Member[]{
                // 默认投资者
                new Member { id = 200001, username = "村花", password = "123456", role_type = ROLE_TYPES.Investor, special_permission = "+100010" },
                // 默认融资者
                new Member { id = 200002, username = "大胜", password = "123456", role_type = ROLE_TYPES.Financier, special_permission = "+100010" }
            };
        }
    }

    class ROLE_TYPES
    {
        public static string Investor = "investor";
        public static string Financier = "financier";
    }
}